package com.example.dictionary.controller;

import com.example.dictionary.service.DomainWordsDictParser;
import com.example.memento.model.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * DomainWordsDict 解析控制器
 */
@RestController
@RequestMapping("/api/domain-words-dict")
public class DomainWordsDictController {
    
    private static final Logger log = LoggerFactory.getLogger(DomainWordsDictController.class);
    
    private final DomainWordsDictParser parser;
    
    public DomainWordsDictController(DomainWordsDictParser parser) {
        this.parser = parser;
    }
    
    /**
     * 解析所有领域文件
     */
    @PostMapping("/parse")
    public ResponseEntity<ApiResponse<Map<String, Object>>> parseAll() {
        try {
            log.info("开始解析 DomainWordsDict 所有领域数据");
            parser.parse();
            
            return ResponseEntity.ok(ApiResponse.<Map<String, Object>>builder()
                    .success(true)
                    .message("解析成功")
                    .data(Map.of("status", "completed"))
                    .build());
        } catch (IllegalStateException e) {
            log.error("解析失败：资源未下载", e);
            return ResponseEntity.badRequest().body(
                    ApiResponse.<Map<String, Object>>builder()
                            .success(false)
                            .message("解析失败：" + e.getMessage())
                            .data(Map.of("error", e.getMessage()))
                            .build()
            );
        } catch (Exception e) {
            log.error("解析 DomainWordsDict 失败", e);
            return ResponseEntity.internalServerError().body(
                    ApiResponse.<Map<String, Object>>builder()
                            .success(false)
                            .message("解析失败：" + e.getMessage())
                            .data(null)
                            .build()
            );
        }
    }
    
    /**
     * 解析指定领域文件
     * 
     * @param domainName 领域名称（不含 .txt 扩展名）
     */
    @PostMapping("/parse/{domainName}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> parseDomain(@PathVariable String domainName) {
        try {
            log.info("开始解析领域: {}", domainName);
            parser.parseDomain(domainName);
            
            return ResponseEntity.ok(ApiResponse.<Map<String, Object>>builder()
                    .success(true)
                    .message("解析成功")
                    .data(Map.of("domain", domainName, "status", "completed"))
                    .build());
        } catch (IllegalStateException | java.io.IOException e) {
            log.error("解析领域失败: {}", domainName, e);
            return ResponseEntity.badRequest().body(
                    ApiResponse.<Map<String, Object>>builder()
                            .success(false)
                            .message("解析失败：" + e.getMessage())
                            .data(Map.of("domain", domainName, "error", e.getMessage()))
                            .build()
            );
        } catch (Exception e) {
            log.error("解析领域失败: {}", domainName, e);
            return ResponseEntity.internalServerError().body(
                    ApiResponse.<Map<String, Object>>builder()
                            .success(false)
                            .message("解析失败：" + e.getMessage())
                            .data(null)
                            .build()
            );
        }
    }
}

